<script>
	export default {
		data() {
			return {
				base_url: '',
				isChecked:true
			}
		},
		onLaunch: function() {
			const that = this
			this.base_url = uni.$baseUrl; 
			uni.hideToast();
			this.checkUpdate();
		}, 
		methods: {  
			async checkLogin(){ 
				if (this.isChecked && uni.getStorageSync(uni.$Call.token) != '') {
					const res = await uni.$api.loginCheck();   
					let pages = getCurrentPages();   
					let page = pages[pages.length - 1]; 
					if(res.data.code == 501  || uni.getStorageSync("resetPassword") == '1'){
						//密码是默认的需要进行修改
						uni.switchTab({
							url:  '/pages/password/setANewPassword'  
						});
						return;
					}
				
				 	if (res.data.code === 0 && 'pages/land/land' == page.route) { 
						/* if(pages.length >= 2){
							uni.switchTab({
								url:   pages[pages.length - 2].route 
							});
						} else { */
							uni.switchTab({
								url:  '/pages/supervise/supervise'  
							});
						//}
						
					} 
				
				}
			},
			async checkUpdate() { 
				const that = this;
				//检查客户端版本 
				let type = uni.getSystemInfoSync().platform
				const res = await uni.$api.updateVersion({
					type : type
				});  
				const response = res.data;
				if (response.data != null) {
					const updateData = response.data;
					let versionOne = 0
					let versionTwo = 0
					let versionThree = 0
				
					if (updateData.packageType == null || updateData.packageType == 1) {
						//全量包
						versionOne = plus.runtime.version.split(".")[0]
						versionTwo = plus.runtime.version.split(".")[1]
						versionThree = plus.runtime.version.split(".")[2] 
						if (updateData.versionOne > versionOne) {
							that.showUpdateModel('0', type, response.data);
						} else if (updateData.versionOne >= versionOne && updateData.versionTwo >
							versionTwo) {
							that.showUpdateModel('0', type, response.data);
						} else if (updateData.versionOne >= versionOne && updateData.versionTwo >=
							versionTwo && updateData.versionThree > versionThree) {
							that.showUpdateModel('0', type, response.data);
						} else {
							that.checkLogin();
						}
					} else {
						//wgt包 
						plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
							uni.setStorageSync("version", widgetInfo.version);
							versionOne = widgetInfo.version.split(".")[0]
							versionTwo = widgetInfo.version.split(".")[1]
							versionThree = widgetInfo.version.split(".")[2] 
							if (updateData.versionOne > versionOne) {
								that.showUpdateModel('1', type, response.data);
							} else if (updateData.versionOne >= versionOne && updateData
								.versionTwo >
								versionTwo) {
								that.showUpdateModel('1', type, response.data);
							} else if (updateData.versionOne >= versionOne && updateData
								.versionTwo >=
								versionTwo && updateData.versionThree > versionThree) {
								that.showUpdateModel('1', type, response.data);
							} else {
								that.checkLogin();
							}
						})
					}
				} else {
					that.checkLogin();
				}
			 
			},
			showUpdateModel(packageType, centerType, data) {
				const that = this;
				uni.removeStorageSync("appupdate");
				uni.showModal({
					title: "有新版本 v" + data.versionOne + "." + data.versionTwo + "." + data.versionThree,
					content: data.content,
					confirmText: '开始更新',
					success: function(r) {
						if (r.confirm) {
							//如果是热更新包 可直接进行更新
							if (packageType == '1') {
								//wgt 
								that.downWgt(that.base_url + data.fileUrl); //下载文件,安装app 
							} else {
								if (centerType == 'android') {
									//apk
									plus.runtime.openURL(that.base_url + data.fileUrl);
								} else {
									let appleId = 'com.chenglian.supervies'
									plus.runtime.launchApplication({
										action: 'itms-apps://itunes.apple.com/cn/app/id' +
											appleId + '?mt=8'
									}, function(e) {
										uni.showModal({
											showCancel: false,
											content: '打开App Store失败,可尝试手动打开App Store搜索此应用进行更新'
										})
									});
								}
							}
						} else {
							//存储有更新信息
							that.checkLogin();
						}
					}
				})
			},
			downWgt: function(downloadUrl) {
				var waiting = plus.nativeUI.showWaiting("开始下载更新文件");
				var progress = 0;
				var task = uni.downloadFile({
					url: downloadUrl,
					success: function(res) {
						if (res.statusCode == 200) {
							//下载完成进行安装
							waiting = plus.nativeUI.showWaiting("开始安装更新文件");
							plus.runtime.install(res.tempFilePath, {
									force: true
								},
								function() {
									waiting.close();
									uni.showModal({ 
										content:'版本已更新',
										showCancel:false,
										success:function(res){
											if (res.confirm) {
												plus.runtime.restart();
											}  
										}
									}) 
								},
								function(e) {
									waiting.close();
									uni.showModal({
										content: '安装更新失败',
										showCancel: false
									})
								});
						} else {
							waiting.close();
							uni.showModal({
								content: '下载更新文件失败',
								showCancel: false
							})
						}
					},
					fail: function() {
						
						waiting.close();
						uni.showModal({
							content: '下载更新文件失败',
							showCancel: false
						})
					}
				});

				//监听下载
				task.onProgressUpdate(function(downloadRes) {
					if (progress != downloadRes.progress) {
						waiting.setTitle("正在下载" + downloadRes.progress + "%", {
							padding: "10px"
						})
					}
					if (downloadRes.progress == 100) {
						waiting.close()
					}
				});
			},
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>